//
//  SearchHistoryCollectionViewCell.m
//  JPWords
//
//  Created by 梁淑婷 on 2024/7/1.
//

#import "SearchHistoryCollectionViewCell.h"

@interface SearchHistoryCollectionViewCell()
@property (nonatomic, strong) UILabel *label;
@end

@implementation SearchHistoryCollectionViewCell

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.layer.cornerRadius = self.height/2.0;
        self.layer.masksToBounds = YES;
        self.layer.borderWidth = 0.5;
        self.layer.borderColor = UIColorRGB(220, 220, 220).CGColor;
        self.backgroundColor = [UIColor whiteColor];
        self.contentView.backgroundColor = [UIColor whiteColor];
        self.label = [[UILabel alloc] init];
        self.label.textAlignment = NSTextAlignmentCenter;
        self.label.numberOfLines = 0;
        self.label.translatesAutoresizingMaskIntoConstraints = NO;
        self.label.font = JPUIFontNotoSansJPRegular(14);
        self.label.textColor = KFontGrayColor;
        [self.contentView addSubview:self.label];
        [self.label mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.bottom.top.mas_equalTo(@0);
        }];
    }
    return self;
}

-(void)setText:(NSString *)text{
    _text = text;
    NSString *language = [self detectLanguageForText:text];
    if([language isEqualToString:@"ja-JP"]||[language isEqualToString:@"ja"]){
        self.label.font = JPUIFontNotoSansJPRegular(14);
    }else{
        self.label.font = JPUIFontPingFangSCRegular(14);
    }
    self.label.text = text;
}

- (NSString *)detectLanguageForText:(NSString *)text{
    if (@available(iOS 11.0, *)) {
        NSLinguisticTagger *tagger = [[NSLinguisticTagger alloc] initWithTagSchemes:@[NSLinguisticTagSchemeLanguage] options:0];
        [tagger setString:text];
        NSString *language = [tagger dominantLanguage];
        return language;
    } else {
        return @"ja-JP";
    }
}

@end
